﻿using ConfExpert.Forms.Base;
using DevExpress.XtraGrid;
using DevTeam;

using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class FAO : XForm
    {
        public FAO()
        {
            InitializeComponent();
        }

        BindingSource bsFAOGrup,bsFAO, bsFAOCustomer,bsFAOBrand;
        private void FAO_Load(object sender, EventArgs e)
        {
            
            TAM.FAOGrupTableAdapter = new Datasets.MyDataTableAdapters.FAOGrupTableAdapter();
            TAM.FAOTableAdapter = new Datasets.MyDataTableAdapters.FAOTableAdapter();
            TAM.FAOCustomerTableAdapter = new Datasets.MyDataTableAdapters.FAOCustomerTableAdapter();
            TAM.FAOBrandTableAdapter = new Datasets.MyDataTableAdapters.FAOBrandTableAdapter();

            bsFAOGrup = new BindingSource(ds, "FAOGrup");
            bsFAO = new BindingSource(bsFAOGrup, "FK_FAO_FAOGrup_FAOGrupId");
            bsFAOCustomer = new BindingSource(bsFAOGrup, "FK_FAOCustomer_FAOGrup_FAOGrupId");
            bsFAOBrand = new BindingSource(bsFAOGrup, "FK_FAOBrand_FAOGrup_FAOGrupId");
            AddBsToList(bsFAO, bsFAOGrup, bsFAOBrand, bsFAOCustomer);

            InitGridView(gvList, bsFAOGrup);
            InitGridView(gvFAO, bsFAO);
            InitGridView(gvCustomer, bsFAOCustomer);
            InitGridView(gvBrand, bsFAOBrand);

            gvBrand.MakeColumnInvisible("FAOGrupId", "FAOBrandId", "UserId");
            gvCustomer.MakeColumnInvisible("FAOGrupId", "FAOCustomerId", "UserId");
            gvFAO.MakeColumnInvisible("1-10", "11-100", "501-");
            gvFAO.SetCaptions("101-500:Oran (%)");

            gvList.OptionsView.NewItemRowPosition = gvCustomer.OptionsView.NewItemRowPosition = gvBrand.OptionsView.NewItemRowPosition = 
                DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            GetDetails("FAOGrup;FAO;FAOCustomer;FAOBrand", "GetFAOs",WhoAmI);

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gvFAO.ShowingEditor += new CancelEventHandler(gvFAO_ShowingEditor);
            gvList.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gvList_FocusedRowChanged);
            EditableGrids();

            splitContainerControl2.AutoSize();
            ds.FAOGrup.TableNewRow += new System.Data.DataTableNewRowEventHandler(FAOGrup_TableNewRow);

            //gvList.ActiveFilterString = "UserId = '" + AppSession.Lookups.V_User.FindByUserId(WhoAmI).Username + "'";
            gvList.BestFitColumns();
            gvFAO.BestFitColumns();
            gvCustomer.BestFitColumns();
            gvBrand.BestFitColumns();
            
            EditableGrids();
        }

        void gvList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            EditableGrids();
        }

        void FAOGrup_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["UserId"] = WhoAmI;
        }

        private void EditableGrids()
        {
            txtInfo.Caption = strParse(bsFAOGrup, "Name");
            gvList.OptionsBehavior.Editable =  gvCustomer.OptionsBehavior.Editable = gvBrand.OptionsBehavior.Editable =
                (intParse(bsFAOGrup, "UserId") == WhoAmI) || 
                gvList.FocusedRowHandle == GridControl.NewItemRowHandle;
        }

        void gvFAO_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = gvFAO.FocusedColumn.FieldName == "ProductCategoryId";
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvBrand.EndEditing();
            gvCustomer.EndEditing();
            gvFAO.EndEditing();
            gvList.EndEditing();
            Save();
            GetDetails("FAOGrup;FAO;FAOCustomer;FAOBrand", "UpdFAOs", WhoAmI);

        }
    }
}